const newRequire = require
newRequire('@babel/register')({
  configFile: false,
  presets: [
    '@babel/preset-env',
    '@babel/preset-typescript'
  ],
  ignore: [],
  extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
});

module.exports = function (api) {
  api.cache(false)
  let presetEnvConfig = {}
  let toEs5Plugins = []
  if(process.env.VUE_APP_BUILD === 'es5') {
    presetEnvConfig = {
      "targets": "> 0.25%, not dead, IE 11", // 目标浏览器（包含 IE11 等需要 ES5 的环境）
      //这个的含义是引入的@babel/polyfill按需加载，只使用项目中需要转为es5的部分，减少项目大小
      // useBuiltIns: "usage",
      // corejs: 3,
    }
    toEs5Plugins.push(
      ["@babel/plugin-transform-arrow-functions", {"spec": true}]
    )
  }
  return {
    presets: [
      ['@vue/babel-preset-app'],
      [
        "@babel/preset-env",
        presetEnvConfig,
      ],
    ],
    "plugins": [
      ["@babel/plugin-proposal-decorators", {"legacy": true}],
      ["@babel/plugin-proposal-class-properties", {"loose": true}],
      ["@babel/plugin-proposal-private-methods", {"loose": true}],
      ["@babel/plugin-proposal-private-property-in-object", {"loose": true}],
      // 转换class语法
      ["@babel/plugin-transform-class-properties", {"loose": true}],
      [newRequire('./babelPlugin/chineseTranscode/chineseTranscode.ts')],
      ["@babel/plugin-proposal-optional-chaining"],
      ...toEs5Plugins,
    ],
  }
}
